﻿using DataAccess.DTO;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;

namespace DataAccess.DAL
{
    public class SysConfigDAL : BaseDAL
    {
        public SysConfig Get(int id)
        {
            return CONN_TopicSharing.Get<SysConfig>(id);
        }

        public SysConfig GetByDomin(string domain)
        {
            //if (!RedisHelper.Exist(TagsKey))
            //{
            //    RedisHelper.Single_Set_Itme<List<Tags>>(TagsKey, CONN_TopicSharing.SQLQuery<Tags>("select * from Tags where state=1", null).ToList());
            //}
            //return RedisHelper.Single_Get_Itme<List<Tags>>(TagsKey);



            List<SysConfig> sysConfigs = CONN_TopicSharing.SQLQuery<SysConfig>(" select  * from SysConfig  where domain=@domain ", new { domain = domain }).ToList();
            if (sysConfigs == null)
            {
                return null;
            }
            return sysConfigs.FirstOrDefault();
        }

        public SysConfig Get_PromoterId(int promoter_id)
        {
            List<SysConfig> sysConfigs = CONN_TopicSharing.SQLQuery<SysConfig>(" select  * from SysConfig  where promoter_id=@promoter_id ", new { promoter_id = promoter_id }).ToList();
            if (sysConfigs == null)
            {
                return null;
            }
            return sysConfigs.FirstOrDefault();
        }

        public bool Insert(SysConfig SysConfig)
        {
            return CONN_TopicSharing.Insert<SysConfig>(SysConfig);
        }

        public bool Update(SysConfig SysConfig)
        {
            return CONN_TopicSharing.Update<SysConfig>(SysConfig);
        }

        public SysConfig GetSysConfig()
        {
            List<SysConfig> sysConfigs = CONN_TopicSharing.SQLQuery<SysConfig>(" select top 1 * from SysConfig  ", null).ToList();
            if (sysConfigs==null)
            {
                return null;
            }
            return sysConfigs.FirstOrDefault();
        }



        public List<SysConfig> GetSysConfigs(string strWhere, string orderbys, int pagesize, int pageindex, out int count)
        {
            List<SysConfig> SysConfigs = new List<SysConfig>();
            if (string.IsNullOrEmpty(strWhere))
            {
                strWhere = " 1=1 ";
            }

            if (string.IsNullOrEmpty(orderbys))
                orderbys = " id desc ";

            SysConfigs = CONN_TopicSharing.SQLPager<SysConfig>("*", " SysConfig", strWhere, "", "", orderbys, pagesize, pageindex, out count, null).ToList();

            return SysConfigs;
        }

        public SysConfig_FlockManageDTO GetSysConfigAllModel(int promoter_id)
        {

            List<SysConfig_FlockManageDTO> sysConfigs = CONN_TopicSharing.SQLQuery<SysConfig_FlockManageDTO>(" select s.id s_id,f.id fm_id　, s.promoter_id, rate_all, rate, rate_i, rate_ii, name, domain, turing_key, logo, send_format, send_tail, is_default, add_data, is_delete, tkl_img, def_flock_time, def_send_time, def_imgtext_time, collecting_source_type, tb_open,　  kick_type, kick_word, auto_reply, welcome, search_word,def_pid,root_str  from  [SysConfig] s left join [dbo].[FlockManage] f on  s.promoter_id=f.promoter_id  where s.promoter_id=@promoter_id", new { promoter_id = promoter_id }).ToList();
            if (sysConfigs == null)
            {
                return null;
            }
            return sysConfigs.FirstOrDefault();
        }

    }
}
